package com.hengpeng.itfin.persist.impl;

import java.util.List;

import org.hibernate.annotations.common.util.StringHelper;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;

import com.hengpeng.common.persist.AbstractDao;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.Projections;
import com.hengpeng.common.persist.query.constants.QueryConstants.LockType;
import com.hengpeng.common.persist.query.constants.QueryConstants.QueryType;
import com.hengpeng.itfin.persist.ReceivePlanDao;
import com.hengpeng.itfinbase.persist.ReceivePlan;
import com.hengpeng.itfinbase.persist.ReceivePlan.ReceiveStatus;

@Repository
@Lazy
public class ReceivePlanDaoImpl extends AbstractDao<ReceivePlan> implements ReceivePlanDao
{
	@Override
	public List<ReceivePlan> getRepayReceivePlans(Long borrowinfoId, String batchId, Integer curRecNum,
			ReceiveStatus[] status)
	{
		DynamicQuery query = new DynamicQuery(ReceivePlan.class);
		if (borrowinfoId != null)
		{
			query.eq("borrowInfoId", borrowinfoId);
		}
		if (!StringHelper.isEmpty(batchId))
		{
			query.eq("batchId", batchId);
		}
		if (curRecNum != null)
		{
			query.eq("curRecNum", curRecNum);
		}
		if (status != null && status.length > 0)
		{
			query.inArray("status", status);
		}
		return super.findAllByDynamicQuery(query);
	}

	@Override
	public long countPlans(Long borrowinfoId, String batchId, Integer curRecNum, ReceiveStatus[] status)
	{
		DynamicQuery query = new DynamicQuery(ReceivePlan.class);
		query.addProjection(Projections.count());
		query.setQueryType(QueryType.JDBC);
		if (borrowinfoId != null)
		{
			query.eq("borrowInfoId", borrowinfoId);
		}
		if (!StringHelper.isEmpty(batchId))
		{
			query.eq("batchId", batchId);
		}
		if (curRecNum != null)
		{
			query.eq("curRecNum", curRecNum);
		}
		if (status != null && status.length > 0)
		{
			query.inArray("status", status);
		}
		return super.countByDynamicQuery(query);
	}

	@Override
	public List<ReceivePlan> lockList(Long borrowinfoId, String batchId, Integer curRecNum, ReceiveStatus[] status)
	{
		DynamicQuery query = new DynamicQuery(ReceivePlan.class);
		if (borrowinfoId != null)
		{
			query.eq("borrowInfoId", borrowinfoId);
		}
		if (!StringHelper.isEmpty(batchId))
		{
			query.eq("batchId", batchId);
		}
		if (curRecNum != null)
		{
			query.eq("curRecNum", curRecNum);
		}
		if (status != null && status.length > 0)
		{
			query.inArray("status", status);
		}
		return super.findAllByDynamicQuery(query, LockType.LOCK_WAIT);
	}
}
